from PyQt5 import QtCore
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QMainWindow, QLabel, QPushButton, QFileDialog, QApplication


from Demo.ImageViewer import ImageViewer
from Demo.VideoViewer import VideoViewer
from Demo.CameraViewer import CameraViewer


class MainWindow(QMainWindow):
    """
    主窗口类，实现人脸识别页面的界面和功能。
    """
    def __init__(self):
        """
        初始化主窗口。
        """
        super().__init__()
        self.setWindowTitle('人脸识别页面')  # 设置窗口标题
        self.setGeometry(200, 200, 1600, 1200)  # 设置窗口位置和大小

        # 添加新标签
        self.title_label = QLabel('基于OpenCV的人脸识别标记系统', self)
        self.title_label.setGeometry(500, 20, 600, 100)  # 设置位置和大小，例如在窗口顶部中央
        self.title_label.setAlignment(Qt.AlignCenter)  # 居中对齐

        # 设置字体大小
        font = QFont()
        font.setPointSize(20)  # 设置字体大小为20
        self.title_label.setFont(font)  # 应用字体到标签

        # 初始化标签，用于显示图片和视频
        self.label_qian = QLabel(self)
        self.label_qian.setGeometry(100, 400, 600, 600)
        self.label_qian.setAlignment(Qt.AlignCenter)

        self.label_hou = QLabel(self)
        self.label_hou.setGeometry(900, 400, 600, 600)
        self.label_hou.setAlignment(Qt.AlignCenter)

        # 初始化按钮，用于加载图片、视频，打开摄像头和退出程序
        self.pushButton_tupian = QPushButton('加载图片', self)
        self.pushButton_tupian.setGeometry(250, 200, 200, 60)

        self.pushButton_shipin = QPushButton('加载视频', self)
        self.pushButton_shipin.setGeometry(550, 200, 200, 60)

        self.pushButton_shexiangtou = QPushButton('打开摄像头', self)
        self.pushButton_shexiangtou.setGeometry(850, 200, 200, 60)

        self.pushButton_tuichu = QPushButton('退出使用', self)
        self.pushButton_tuichu.setGeometry(1150, 200, 200, 60)

        # 绑定按钮点击事件
        self.pushButton_tupian.clicked.connect(self.load_image)
        self.pushButton_shipin.clicked.connect(self.load_video)
        self.pushButton_shexiangtou.clicked.connect(self.open_camera)
        self.pushButton_tuichu.clicked.connect(self.exit_system)

        # 初始化图片、视频和摄像头查看器
        self.image_viewer = ImageViewer(self.label_qian, self.label_hou)
        self.video_viewer = VideoViewer(self.label_qian, self.label_hou)
        self.camera_viewer = CameraViewer(self.label_qian, self.label_hou)

    def load_image(self):
        """
        加载图片。
        """
        file_dialog = QFileDialog()  # 创建文件对话框
        file_path, _ = file_dialog.getOpenFileName(None, '选择图片', '', 'Image Files (*.png *.jpg *.bmp)')  # 让用户选择图片
        if file_path:  # 如果用户选择了图片，则加载图片
            self.image_viewer.load_image(file_path)

    def load_video(self):
        """
        加载视频。
        """
        file_dialog = QFileDialog()  # 创建文件对话框
        file_path, _ = file_dialog.getOpenFileName(None, '选择视频', '', 'Video Files (*.mp4 *.avi)')  # 让用户选择视频
        if file_path:  # 如果用户选择了视频，则加载视频
            self.video_viewer.load_video(file_path)

    def open_camera(self):
        """
        打开摄像头。
        """
        self.camera_viewer.open_camera()

    def exit_system(self):
        """
        退出系统。
        """
        self.camera_viewer.close_camera()  # 关闭摄像头
        self.close()  # 关闭窗口


if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()

